package com.hongyun.tms.aspect;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.annotation.excel.ExcelInfo;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.infra.excel.StyleUtils;
import com.hongyun.tms.infra.excel.config.CustomCellWriteHeightConfig;
import com.hongyun.tms.infra.excel.config.CustomCellWriteWidthConfig;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * excel导出切面
 *
 * @author liPeng
 * @date 2024-09-07
 */
@Component
@Aspect
public class ExcelExportAspect {
    @Pointcut("@annotation(com.hongyun.tms.common.annotation.excel.ExcelExport)")
    public void ExcelExportTarget() {
    }

    //定义通知
    @Around("ExcelExportTarget() && @annotation(excelParam)")
    public void doProceed(ProceedingJoinPoint joinPoint, ExcelExport excelParam) throws Throwable {
        //获取导出类型字节码对象
        Class<?> clazz = excelParam.value();
        //-------------------------------修改部分--------------------------------------
        //定义默认文件名与表名
        String fileName = "数据导出";
        String sheetName = "sheet1";
        //获取注解上设定的名字
        boolean isClassAnnotation = clazz.isAnnotationPresent(ExcelInfo.class);
        if (isClassAnnotation) {
            ExcelInfo excelInfo = clazz.getAnnotation(ExcelInfo.class);
            fileName = StringUtils.isBlank(excelInfo.fileName()) ? fileName : excelInfo.fileName();
            sheetName = StringUtils.isBlank(excelInfo.sheetName()) ? sheetName : excelInfo.sheetName();
            ;
        }
        //-----------------------------------------------------------------------------
        //获取参数中的输出流对象
        HttpServletResponse response = null;
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg.getClass());
            if (arg instanceof HttpServletResponse) {
                response = (HttpServletResponse) arg;
            }
        }
        AjaxResult ajaxResult = (AjaxResult) joinPoint.proceed();
        //执行目标方法   获取导出数据
        List data = (List) ajaxResult.getData();
        //设置响应参数
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");


        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(StyleUtils.getHeadStyle(), StyleUtils.getContentStyle());

        //执行导出
        EasyExcel.write(response.getOutputStream(), clazz)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(new CustomCellWriteHeightConfig())
                .registerWriteHandler(new CustomCellWriteWidthConfig())
                .sheet(sheetName)
                .doWrite(data);
    }

}
